package com.yuncommunity.cloudlab.controller.client

import com.yuncommunity.cloudlab.model.User
import com.yuncommunity.cloudlab.repository.UserRepository
import com.yuncommunity.cloudlab.utils.IpUtils
import com.yuncommunity.cloudlab.utils.MyCaptcha
import com.yuncommunity.cloudlab.utils.MyResult
import net.bytebuddy.utility.RandomString
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.util.DigestUtils
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping("client/api/user")
@CrossOrigin("*")
class ClientUser {
    @Autowired
    lateinit var userRepository: UserRepository


    @GetMapping("/login")
    @ResponseBody
    fun login(name: String, password: String, request: HttpServletRequest): MyResult {
        var u = userRepository.findByPhoneAndPassword(name, DigestUtils.md5DigestAsHex(password.toByteArray()))
        return if (u == null) {
            MyResult(1, "账号或密码错误")
        } else {
            if (u.isDelete) {
                return MyResult(5, "账号已被删除")
            }

            u.token = RandomString.make(32)
            u.lastLoginIp = IpUtils.getIpAddr(request)
            u.lastLoginAddress = IpUtils.getAddressResult(u.lastLoginIp)
            u.lastLoginTime = Date()

            userRepository.save(u)
            u.password = ""
            MyResult(0, u)
        }
    }

    @GetMapping("/login_by_phone")
    @ResponseBody
    fun loginByPhone(phone: String?, vCode: String?, request: HttpServletRequest): MyResult {
        if (phone == null) {
            return MyResult(1, "手机号不能为空")
        }
        if (vCode == null) {
            return MyResult(2, "验证码不能为空")
        }
        if (MyCaptcha.smsCodeVerify(phone, vCode)) {
            val user = userRepository.findByPhone(phone)
            if (user == null) {
                return MyResult(3, "手机号码不存在,请先注册")
            }

            if (user.isDelete) {
                return MyResult(5, "账号已被删除")
            }
            user.token = RandomString.make(32)
            user.lastLoginIp = IpUtils.getIpAddr(request)
            user.lastLoginAddress = IpUtils.getAddressResult(user.lastLoginIp)
            user.lastLoginTime = Date()

            userRepository.save(user)
            user.password = ""
            return MyResult(0, user)
        }
        return MyResult(3, "验证码错误")
    }

    @GetMapping("/register")
    @ResponseBody
    fun register(phone: String?, vCode: String?, password: String, request: HttpServletRequest): MyResult {
        if (phone == null) {
            return MyResult(1, "手机号不能为空")
        }
        if (vCode == null) {
            return MyResult(2, "验证码不能为空")
        }
        if (MyCaptcha.smsCodeVerify(phone, vCode)) {
            if (userRepository.existsByPhone(phone)) {
                return MyResult(3, "手机号码已存在,请直接登录")
            }

            var user = User()
            user.phone = phone
            user.password = DigestUtils.md5DigestAsHex(password.toByteArray())
            user.token = RandomString.make(32)
            user.regIp = IpUtils.getIpAddr(request)
            user.regAddress = IpUtils.getAddressResult(user.lastLoginIp)

            userRepository.save(user)
            user.password = ""
            return MyResult(0, user)
        }

        return MyResult(4, "验证码错误")
    }

    @GetMapping("/forget_password")
    @ResponseBody
    fun forgetPassword(phone: String?, vCode: String?, newPassword: String): MyResult {
        if (phone == null) {
            return MyResult(3, "手机号不能为空")
        }
        if (vCode == null) {
            return MyResult(4, "验证码不能为空")
        }
        val user = userRepository.findByPhone(phone)
        if (user == null || user.id == 0L) {
            return MyResult(1, "手机号码不存在")
        }
        if (MyCaptcha.smsCodeVerify(phone, vCode)) {
            user.password = DigestUtils.md5DigestAsHex(newPassword.toByteArray())
            user.token = RandomString.make(32)
            userRepository.save(user)
            return MyResult(user)
        }
        return MyResult(2, "验证码错误")
    }

    /**
     * 获取图形验证码
     *
     * @return
     */
    @GetMapping("/imageCode")
    fun imageCode(): MyResult {
        return MyResult(MyCaptcha.newImageCaptcha())
    }

    @GetMapping("/info")
    @ResponseBody
    fun info(token: String, request: HttpServletRequest): MyResult {
        var u = userRepository.findByToken(token)
        return if (u == null) {
            MyResult(-1, "登录已过期,请重新登录")
        } else {
            if (u.isDelete) {
                return MyResult(5, "账号已被删除")
            }
            u.password = ""
            MyResult(u)
        }
    }

}